// wenshidu/wen_listxq/wen_listxq.js
let app = getApp()
let yuming = app.globalData.yuming
const request = require('../../config/request');
import * as echarts from '../../pages/components/ec-canvass/echarts';
let chart = null;
//获取像素比
const getPixelRatio = () => {
  let pixelRatio = 0
  wx.getSystemInfo({
    success: function (res) {
      pixelRatio = res.pixelRatio
    },
    fail: function () {
      pixelRatio = 0
    }
  })
  return pixelRatio
}
let dpr = getPixelRatio();

Page({
  ...request,
  /**
   * 页面的初始数据
   */
  data: {
    humidity_type:'',
    yuming: yuming,
    title: '设备详情',
    wenxia_gao: '',
    currtab: 0,
    huan: ['详情', '数据', '设置'],
    // huan: ['详情', '数据'],
    startime: '请选择',
    startimes: '',
    endtime: '请选择',
    endtimes: '',
    lie: [],
    page: 1,
    shuju_gao: '',
    zhexian: {
      lazyLoad: true
    },
    nickName: '', //温湿度的名称
    she_id: '', //设备的id
    canis_show: false,
    caiji_jiange_list: [],
    caiji_jiange_list2: [],
    caiji_jiange_guo: '请选择正常采集间隔', //请选择采集间隔
    caiji_jiange_guos: '请选择正常采集间隔', //请选择采集间隔
    caiji_jiange_index: '',
    shangc_jiange_list: [],
    shangc_jiange_list2: [],
    shangc_jiange_guo: '请选择正常上传间隔',
    shangc_jiange_guos: '请选择正常上传间隔',
    shangc_jiange_index: '',
    chaowencj_jiange_list: [],
    chaowencj_jiange_list2: [],
    chaowencj_jiange_guo: '请选择超温采集间隔',
    chaowencj_jiange_guos: '请选择超温采集间隔',
    chaowencj_jiange_index: '',
    chaowensc_jiange_list: [],
    chaowensc_jiange_list2: [],
    chaowensc_jiange_guo: '请选择超温上传间隔',
    chaowensc_jiange_guos: '请选择超温上传间隔',
    chaowensc_jiange_index: '',

    wensx_bj: false, //温度上限报警
    baojingsx_list: [],
    baojingsx_list2: [],
    baojingsx_guo: '请选择报警上限温度',
    baojingsx_guos: '请选择报警上限温度',
    baojingsx_index: '',
    wenxx_bj: false, //温度下限报警
    baojingxx_list: [],
    baojingxx_list2: [],
    baojingxx_guo: '请选择报警下限温度',
    baojingxx_guos: '请选择报警下限温度',
    baojingxx_index: '',

    shisx_bj: false, //湿度上限报警
    baojingsxsd_list: [],
    baojingsxsd_list2: [],
    baojingsxsd_guo: '请选择报警上限湿度',
    baojingsxsd_guos: '请选择报警上限湿度',
    baojingsxsd_index: '',
    shixx_bj: false, //湿度下限报警
    baojingxxsd_list: [],
    baojingxxsd_list2: [],
    baojingxxsd_guo: '请选择报警下限湿度',
    baojingxxsd_guos: '请选择报警下限湿度',
    baojingxxsd_index: '',

    dianlbz_gj: false, //电量不足告警
    binfdianl_gjz: [], //电量告警值
    binfdianl_gjz2: [], //电量告警值
    dianlgjz_guo: '请选择电量告警值',
    dianlgjz_guos: '请选择电量告警值',
    dianlgjz_index: '',

    shezhi_canshu: '',

    shuji: [],
    pagefen_index: 3,
    zongye: '', //总页数
    tishi: '(>60s)',


    calTmp: '', //温度校准
    calHum: '', //湿度校准

    photo: [],
    wuphoto: [],

    shade_cold:''
  },
  bininput(e) {
    var value = e.detail.value
    var madal = e.currentTarget.dataset.modal
    console.log(madal)
    this.setData({
      [madal]: value
    })
  },
  // 放大
  fangda(e) {
    var img = e.currentTarget.dataset.img
    var list = e.currentTarget.dataset.list
    var arr = []
    for (let i in list) {
      arr.push(list[i]); //属性
    }
    wx.previewImage({
      current: img,
      urls: arr
    })
  },
  // 差掉
  chadiao(e) {
    var index = e.currentTarget.dataset.index
    var photo = this.data.photo;
    var wuphoto = this.data.wuphoto;
    photo.splice(index, 1)
    wuphoto.splice(index, 1)
    this.setData({
      photo: photo,
      wuphoto: wuphoto
    })
  },
  // 上传
  chuan(e) {
    var that = this
    var pics = [];
    var wuphoto = this.data.wuphoto;
    this.myChooseImage(9).then(res => {
      console.log(res)
      const tempFilePaths = res.tempFilePaths
      for (var i = 0; i < tempFilePaths.length; i++) {
        pics.push(tempFilePaths[i])
      }
      if ((pics.length) + (that.data.photo.length) > 9) {
        wx.showToast({
          title: '最多可上传' + 9 + "张",
          icon: 'none'
        })
      } else {
        for (var i = 0; i < tempFilePaths.length; i++) {
          (function (i) { // 注意这里是形参
            setTimeout(function () {
              that.duoupimg(tempFilePaths[i]).then(res => {
                wx.showLoading({
                  title: '上传图片中...',
                });
                var data = JSON.parse(res.data)
                var urls = data.httpUrl + data.imgUrl
                var wuurls = data.imgUrl
                // wuphoto = wuphoto.concat(data.imgUrl + ",")
                that.setData({
                  photo: that.data.photo.concat(urls),
                  wuphoto: that.data.wuphoto.concat(wuurls),
                });
                if (i == tempFilePaths.length - 1) {
                  wx.hideLoading();
                }
              })
            }, 800 * i); // 还是每秒执行一次，不是累加的
          })(i) // 注意这里是实参，这里把要用的参数传进去
        }
      }
    })
  },
  // 正常采集间隔
  shuzccjjg(e) {
    this.setData({
      caiji_jiange_guo: e.detail.value,
      caiji_jiange_guos: e.detail.value
    })
  },
  // 正常上传间隔
  bindzcscjg(e) {
    this.setData({
      shangc_jiange_guo: e.detail.value,
      shangc_jiange_guos: e.detail.value
    })
  },
  // 超温采集间隔
  bindcwcjjg(e) {
    this.setData({
      chaowencj_jiange_guo: e.detail.value,
      chaowencj_jiange_guos: e.detail.value
    })
  },
  // 超温上传间隔
  bindcwscjg(e) {
    this.setData({
      chaowensc_jiange_guo: e.detail.value,
      chaowensc_jiange_guos: e.detail.value
    })
  },
  // 页数
  bindyeshu(e) {
    console.log(e)
    this.setData({
      page: e.detail.value
    })
    this.shujua()
  },
  // 上一页
  shang_page() {
    this.setData({
      pagefen_index: 0
    })
    if (this.data.page == 1) {
      wx.showToast({
        title: '没有上一页了',
        icon: 'none'
      })
    } else {
      var page = this.data.page - 1
      this.setData({
        page: page
      })
      this.shujua()
    }
  },
  // 下一页
  xia_page() {
    if (this.data.shuji.length != 10) {
      wx.showToast({
        title: '没有更多数据了',
        icon: 'none'
      })
    } else {
      this.setData({
        pagefen_index: 1
      })
      var page = this.data.page + 1
      this.setData({
        page: page
      })
      this.shujua()
    }

  },
  // 开始
  bindDateChange(e) {
    this.setData({
      startime: e.detail.value,
      startimes: e.detail.value,
      pagefen_index: 3,
      page: 1
    })
    this.shujua()
  },
  // 结束
  bindDateChanges(e) {
    if (e.detail.value < this.data.startime) {
      wx.showToast({
        title: '不能小于开始时间!',
        icon: 'error'
      })
    } else {
      this.setData({
        endtime: e.detail.value,
        endtimes: e.detail.value,
        pagefen_index: 3,
        page: 1
      })
      this.shujua()
    }
  },
  // 采集间隔
  bindcaiji_jiange(e) {
    var index = e.detail.value;
    this.setData({
      caiji_jiange_index: index,
      caiji_jiange_guo: this.data.caiji_jiange_list[index],
      caiji_jiange_guos: this.data.caiji_jiange_list2[index],
    })
  },
  // 上传间隔
  bindshangc_jiange(e) {
    var index = e.detail.value;
    this.setData({
      shangc_jiange_index: index,
      shangc_jiange_guo: this.data.shangc_jiange_list[index],
      shangc_jiange_guos: this.data.shangc_jiange_list2[index],
    })
  },
  // 超温采集间隔
  bindchaowencj_jiange(e) {
    var index = e.detail.value;
    this.setData({
      chaowencj_jiange_index: index,
      chaowencj_jiange_guo: this.data.chaowencj_jiange_list[index],
      chaowencj_jiange_guos: this.data.chaowencj_jiange_list2[index],
    })
  },
  // 超温上传间隔
  bindchaowensc_jiange(e) {
    var index = e.detail.value;
    this.setData({
      chaowensc_jiange_index: index,
      chaowensc_jiange_guo: this.data.chaowensc_jiange_list[index],
      chaowensc_jiange_guos: this.data.chaowensc_jiange_list2[index],
    })
  },

  // 温度上限报警
  bindwensx_bj(e) {
    var value = e.detail.value
    this.setData({
      wensx_bj: !this.data.wensx_bj
    })
  },
  // 报警上限温度
  bindbaojingsx(e) {
    var index = e.detail.value;
    this.setData({
      baojingsx_index: index,
      baojingsx_guo: this.data.baojingsx_list[index],
      baojingsx_guos: this.data.baojingsx_list2[index],
    })
  },
  // 温度下限报警
  bindwenxx_bj(e) {
    var value = e.detail.value
    this.setData({
      wenxx_bj: !this.data.wenxx_bj
    })
  },
  // 报警下限温度
  bindbaojingxx(e) {
    var index = e.detail.value;
    this.setData({
      baojingxx_index: index,
      baojingxx_guo: this.data.baojingxx_list[index],
      baojingxx_guos: this.data.baojingxx_list2[index],
    })
  },

  // 湿度上限报警
  bindshisx_bj(e) {
    var value = e.detail.value
    this.setData({
      shisx_bj: !this.data.shisx_bj
    })
  },
  // 报警上限湿度
  bindbaojingsxsd(e) {
    var index = e.detail.value;
    this.setData({
      baojingsxsd_index: index,
      baojingsxsd_guo: this.data.baojingsxsd_list[index],
      baojingsxsd_guos: this.data.baojingsxsd_list2[index],
    })
  },
  // 湿度下限报警
  bindshixx_bj(e) {
    var value = e.detail.value
    this.setData({
      shixx_bj: !this.data.shixx_bj
    })
  },
  // 报警下限湿度
  bindbaojingxxsd(e) {
    var index = e.detail.value;
    this.setData({
      baojingxxsd_index: index,
      baojingxxsd_guo: this.data.baojingxxsd_list[index],
      baojingxxsd_guos: this.data.baojingxxsd_list2[index],
    })
  },


  // 电量不足告警
  binddianlbz_gj(e) {
    var value = e.detail.value;
    this.setData({
      dianlbz_gj: !this.data.dianlbz_gj
    })
  },
  nickName(e) {
    var value = e.detail.value;
    this.setData({
      nickName: value
    })
  },
  // 电量告警值
  binddianlgjz(e) {
    var index = e.detail.value;
    this.setData({
      dianlgjz_index: index,
      dianlgjz_guo: this.data.binfdianl_gjz[index],
      dianlgjz_guos: this.data.binfdianl_gjz2[index],
    })
  },



  // 切换
  qies(e) {
    var index = e.currentTarget.dataset.currtab
    this.setData({
      currtab: index,
      page: 1,
      pagefen_index: 3
    })
    if (index == 0) {
      this.setData({
        canis_show: false
      })
      this.tubiao()
    }
    if (index == 1) {
      this.shujua()
      this.setData({
        canis_show: true
      })
    }
    if (index == 2) {
      this.wenxq()
      this.setData({
        canis_show: true
      })
    }
  },
  setOption: function (chart, option) {
    var option = option;
    console.log(option)
    chart.setOption(option);
  },
  // 返回
  backs() {
    wx.navigateBack({
      delta: 0
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    var humidity_type = options.humidity_type
    var  huan= ['详情', '数据']
    if(humidity_type==1){
      this.setData({
        huan:huan
      })
    }
    this.setData({
      heights: wx.getMenuButtonBoundingClientRect().top + 5,
      wenxia_gao: wx.getMenuButtonBoundingClientRect().top + 80,
      shuju_gao: wx.getMenuButtonBoundingClientRect().top + 120,
      she_id: options.she_id,
      wen_id: options.wenid,
      shade_cold:options.shade_cold,
      humidity_type:options.humidity_type
    })
    this.firstComponent = this.selectComponent('#mychart-dom-line');
    // this.oneComponent = this.selectComponent('#mychart-dom-bar0');
    // 时间：10分钟-24小时，
    // 温度：-40-295
    // 湿度：10-98
    // 电量：30
    // 正常采集间隔
    var caiji_jiange_list = []
    var caiji_jiange_list2 = []

    var caiji_jiange_list3 = []
    var caiji_jiange_list4 = []
    for (var i = 10; i <= 59; i++) {
      caiji_jiange_list.push(i + '分钟')
      caiji_jiange_list3.push(i)
    }
    // for (var i = 1; i <= 24; i++) {
    //   caiji_jiange_list2.push(i + '小时')
    //   caiji_jiange_list4.push(i)
    // }
    var shijianarr = []
    var shijianarr2 = []
    // shijianarr = [...caiji_jiange_list, ...caiji_jiange_list2]
    // shijianarr2 = [...caiji_jiange_list3, ...caiji_jiange_list4]
    shijianarr = caiji_jiange_list
    shijianarr2 = caiji_jiange_list3
    this.setData({
      caiji_jiange_list: shijianarr,
      caiji_jiange_list2: shijianarr2,
      shangc_jiange_list: shijianarr,
      shangc_jiange_list2: shijianarr2,
      chaowencj_jiange_list: shijianarr,
      chaowencj_jiange_list2: shijianarr2,
      chaowensc_jiange_list: shijianarr,
      chaowensc_jiange_list2: shijianarr2
    })

    // 超温采集间隔
    var chaowencj_jiange_list = []
    var chaowencj_jiange_list2 = []
    for (var i = -40; i <= 295; i++) {
      chaowencj_jiange_list.push(i + ' ℃')
      chaowencj_jiange_list2.push(i)
    }
    this.setData({
      baojingsx_list: chaowencj_jiange_list,
      baojingsx_list2: chaowencj_jiange_list2,
      baojingxx_list: chaowencj_jiange_list,
      baojingxx_list2: chaowencj_jiange_list2,
    })
    // 湿度
    var baojingsxsd_list = []
    var baojingsxsd_list2 = []
    for (var i = 10; i <= 98; i++) {
      baojingsxsd_list.push(i + '%RH')
      baojingsxsd_list2.push(i)
    }
    this.setData({
      baojingsxsd_list: baojingsxsd_list,
      baojingsxsd_list2: baojingsxsd_list2,
      baojingxxsd_list: baojingsxsd_list,
      baojingxxsd_list2: baojingsxsd_list2,
    })
    this.setData({
      binfdianl_gjz: ['30%'],
      binfdianl_gjz2: ['30%'],
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },
  // 分钟转换秒
  // formatHMStoS(value) {
  //   console.log(value)
  //   var min = value * 60;
  //   console.log(min)
  //   return min;
  // },
  // 秒数转分钟
  formatSeconds(value) {
    var secondTime = parseInt(value); // 秒
    var minuteTime = 0; // 分
    var hourTime = 0; // 小时
    if (secondTime > 60) { //如果秒数大于60，将秒数转换成整数
      //获取分钟，除以60取整数，得到整数分钟
      minuteTime = parseInt(secondTime / 60);
      //获取秒数，秒数取佘，得到整数秒数
      secondTime = parseInt(secondTime % 60);
      //如果分钟大于60，将分钟转换成小时
      if (minuteTime > 60) {
        //获取小时，获取分钟除以60，得到整数小时
        hourTime = parseInt(minuteTime / 60);
        //获取小时后取佘的分，获取分钟除以60取佘的分
        minuteTime = parseInt(minuteTime % 60);
      }
    }
    var result = "";
    // var result = "" + parseInt(secondTime) + "秒";

    // if (minuteTime > 0) {
    result = "" + parseInt(minuteTime) + result;
    // }
    // if (hourTime > 0) {
    //   result = "" + parseInt(hourTime) + "小时" + result;
    // }
    return result;
  },
  // 存储设备列表接口
  wenxq() {
    wx.showLoading({
      title: '加载中',
      icon: 'none'
    })
    let that = this
    this.myRequest(yuming + 'Humidity/earlywarning_device_info', {
      shopId: wx.getStorageSync('shopId'),
      id: that.data.she_id
    }, "POST").then(res => {
      console.log(res)
      if (res.data.code == 200) {
        var data = res.data.data
        this.setData({
          shezhi_canshu: data,
          // caiji_jiange_guo: that.formatSeconds(data.collectionInterval) + ' 分钟',
          // caiji_jiange_guos: that.formatSeconds(data.collectionInterval),
          // caiji_jiange_guo: data.collectionInterval,
          // caiji_jiange_guos: data.collectionInterval,
          // shangc_jiange_guo: data.uploadInterval,
          // shangc_jiange_guos: data.uploadInterval,
          // chaowencj_jiange_guo: data.overCollectionInterval,
          // chaowencj_jiange_guos: data.overCollectionInterval,
          // chaowensc_jiange_guo: data.overUploadInterval,
          // chaowensc_jiange_guos: data.overUploadInterval,
          // baojingsx_guo: data.tmpUpperLimit + ' ℃',
          // baojingsx_guos: data.tmpUpperLimit,
          // baojingxx_guo: data.tmpLowerLimit + ' ℃',
          // baojingxx_guos: data.tmpLowerLimit,
          // baojingsxsd_guo: data.humUpperLimit + ' %RH',
          // baojingsxsd_guos: data.humUpperLimit,
          // baojingxxsd_guo: data.humLowerLimit + ' %RH',
          // baojingxxsd_guos: data.humLowerLimit,
          // dianlgjz_guo: data.quantityLimit + ' %',
          // dianlgjz_guos: data.quantityLimit,

          // calHum: data.calHum,
          // calTmp: data.calTmp,
        })
        if (data.nickName == null || data.nickName == undefined) {
          this.setData({
            nickName: '',
          })
        } else {
          this.setData({
            nickName: data.nickName,
          })
        }
        // 0否1是
        // 报警上限温度
        if (data.enableTmpUpperAlarm == 0) {
          this.setData({
            wensx_bj: false
          })
        }
        if (data.enableTmpUpperAlarm == 1) {
          this.setData({
            wensx_bj: true
          })
        }
        // 报警下限温度
        if (data.enableTmpLowerAlarm == 0) {
          this.setData({
            wenxx_bj: false
          })
        }
        if (data.enableTmpLowerAlarm == 1) {
          this.setData({
            wenxx_bj: true
          })
        }
        // 湿度上限报警
        if (data.enableHumUpperAlarm == 0) {
          this.setData({
            shisx_bj: false
          })
        }
        if (data.enableHumUpperAlarm == 1) {
          this.setData({
            shisx_bj: true
          })
        }
        // 湿度下限报警
        if (data.enableHumLowerAlarm == 0) {
          this.setData({
            shixx_bj: false
          })
        }
        if (data.enableHumLowerAlarm == 1) {
          this.setData({
            shixx_bj: true
          })
        }
        // 电量不足
        if (data.enableQuantityAlarm == 0) {
          this.setData({
            dianlbz_gj: false
          })
        }
        if (data.enableQuantityAlarm == 1) {
          this.setData({
            dianlbz_gj: true
          })
        }
      } else {
        wx.showToast({
          title: res.data.msg,
          icon: 'none'
        })
      }

    })
  },
  /**
   * 生命周期函数--监听页面显示
   */
  // dingyue() {
  //   var that = this
  //   // console.log(wx.getStorageSync('wen_is_ding') == 0)
  //   // if (wx.getStorageSync('wen_is_ding') == 0) {
  //   // 这里是获取下发权限地方，根据官方文档，可以根据  wx.getSetting() 的 withSubscriptions   这个参数获取用户是否打开订阅消息总开关。后面我们需要获取用户是否同意总是同意消息推送。所以这里要给它设置为true 。
  //   wx.getSetting({
  //     withSubscriptions: true, //  这里设置为true,下面才会返回mainSwitch
  //     success: function (res) {
  //       console.log(res)
  //       // 调起授权界面弹窗 
  //       if (res.subscriptionsSetting.mainSwitch) { // 用户打开了订阅消息总开关
  //         if (res.subscriptionsSetting.itemSettings != null) { // 用户同意总是保持是否推送消息的选择, 这里表示以后不会再拉起推送消息的授权
  //           let moIdState = res.subscriptionsSetting.itemSettings['f5VhPUbj51dhls-w-ruVnM5mkZ7HfZ2zqaUps-1tolY', 'f5VhPUbj51dhls-w-ruVnKKomc7IFXExpjFCKrrJwsQ']; // 用户同意的消息模板id
  //           if (moIdState === 'accept') {
  //             console.log('接受了消息推送');

  //           } else if (moIdState === 'reject') {
  //             console.log("拒绝消息推送");

  //           } else if (moIdState === 'ban') {
  //             console.log("已被后台封禁");

  //           }
  //         } else {
  //           if (!wx.getStorageSync('token')) {
  //             wx.showModal({
  //               title: '订阅监管消息需要授权哦~',
  //               cancelText: '取消',
  //               confirmText: '前往',
  //               success: function (res) {
  //                 if (res.confirm) {
  //                   console.log("确定")
  //                   wx.navigateTo({
  //                     url: '/page_zhu/shou/shou?sta=66',
  //                   })
  //                 } else {
  //                   // 0 不弹 1弹
  //                   wx.setStorageSync('wen_istan', 1)
  //                 }
  //               }
  //             })
  //           } else {
  //             // 当用户没有点击 ’总是保持以上选择，不再询问‘  按钮。那每次执到这都会拉起授权弹窗
  //             wx.showModal({
  //               title: '提示',
  //               content: '请授权开通服务通知',
  //               showCancel: true,
  //               success: function (ress) {
  //                 if (ress.confirm) {
  //                   wx.requestSubscribeMessage({ // 调起消息订阅界面
  //                     tmplIds: ['f5VhPUbj51dhls-w-ruVnM5mkZ7HfZ2zqaUps-1tolY', 'f5VhPUbj51dhls-w-ruVnKKomc7IFXExpjFCKrrJwsQ'],
  //                     success(res) {
  //                       console.log('订阅消息 成功 ');
  //                       console.log(res);
  //                       if (res["f5VhPUbj51dhls-w-ruVnM5mkZ7HfZ2zqaUps-1tolY"] === "accept") {
  //                         wx.request({
  //                           url: yuming + 'Besall/subscribe_save',
  //                           method: 'POST',
  //                           header: {
  //                             "content-type": "application/x-www-form-urlencoded",
  //                             token: wx.getStorageSync('token'),
  //                           },
  //                           data: {

  //                           },
  //                           success: function (res) {
  //                             console.log(res)
  //                             if (res.data.code == 200) {
  //                               // wx.setStorageSync('wen_is_ding', 1)
  //                               // wx.setStorageSync('wen_istan', 1)
  //                             } else {
  //                               // wx.setStorageSync('wen_istan', 0)
  //                               // wx.setStorageSync('wen_is_ding', 0)
  //                             }
  //                           },
  //                           fail: function () {
  //                             console.log("请求数据失败");
  //                           }
  //                         })
  //                       }
  //                     },
  //                     fail(er) {
  //                       console.log("订阅消息 失败 ");
  //                       console.log(er);
  //                     }
  //                   })

  //                 }
  //               }
  //             })
  //           }

  //         }

  //       } else {
  //         console.log('订阅消息未开启')
  //       }
  //     },
  //     fail: function (error) {
  //       console.log(error);
  //     },
  //   })

  // },


  dingyue() {
    wx.getSetting({
      withSubscriptions: true,
      success(res) {
        console.log(res)
        console.log(res.subscriptionsSetting)
        if (res.subscriptionsSetting.mainSwitch) {
          // 点了总是保持
          console.log(111111111)
          wx.showModal({
            title: '开启温湿度订阅消息',
            cancelText: '取消',
            confirmText: '确定',
            success: function (res) {
              if (res.confirm) {
                console.log("确定")
                wx.requestSubscribeMessage({ // 调起消息订阅界面
                  tmplIds: ['8lQiCwqP_ehbnSWfAwcGXwnEwaw2_KWX1bqttap1bK0', 'PIWKcwKwVuHIZA6IC8bXBPIZQ0BYY6T2Vp36Xc_obtY'],
                  success(res) {
                    console.log(res)
                    if (res["8lQiCwqP_ehbnSWfAwcGXwnEwaw2_KWX1bqttap1bK0"] === "accept" || res["PIWKcwKwVuHIZA6IC8bXBPIZQ0BYY6T2Vp36Xc_obtY"] === "accept") {
                      console.log(res);
                      wx.request({
                        url: yuming + 'Besall/subscribe_save',
                        method: 'POST',
                        header: {
                          "content-type": "application/x-www-form-urlencoded",
                          token: wx.getStorageSync('token'),
                        },
                        data: {

                        },
                        success: function (res) {
                          console.log(res)
                          if (res.data.code == 200) {
                            wx.setStorageSync('wen_is_ding', 1)
                            wx.setStorageSync('wen_istan', 1)
                          } else {
                            wx.showToast({
                              title: res.data.msg,
                              icon: 'none'
                            })
                            wx.navigateTo({
                              url: '/page_zhu/shou/shou?sta=66',
                            })
                            wx.setStorageSync('wen_istan', 0)
                            wx.setStorageSync('wen_is_ding', 0)
                          }
                        },
                        fail: function () {
                          console.log("请求数据失败");
                        }
                      })
                    } else {
                      wx.setStorageSync('wen_istan', 1)
                    }
                  },
                  fail(er) {
                    console.log("订阅消息 失败 ");
                    console.log(er);
                    wx.setStorageSync('wen_is_ding', 0)
                    wx.setStorageSync('wen_istan', 0)
                  }
                })
              } else {
                // 0 不弹 1弹
                wx.setStorageSync('wen_istan', 1)
              }
            }
          })

        } else {
          console.log(22222222222222)
          var that = this
          if (!wx.getStorageSync('token')) {
            wx.showModal({
              title: '订阅监管消息需要授权哦~',
              cancelText: '取消',
              confirmText: '前往',
              success: function (res) {
                if (res.confirm) {
                  console.log("确定")
                  wx.navigateTo({
                    url: '/page_zhu/shou/shou?sta=66',
                  })
                } else {
                  // 0 不弹 1弹
                  wx.setStorageSync('wen_istan', 1)
                }
              }
            })
          } else {
            console.log(wx.getStorageSync('wen_is_ding') == 0)
            if (wx.getStorageSync('wen_is_ding') == 0) {
              wx.showModal({
                title: '提示',
                content: '请授权开通服务通知',
                showCancel: true,
                success: function (ress) {
                  if (ress.confirm) {
                    wx.requestSubscribeMessage({ // 调起消息订阅界面
                      tmplIds: ['8lQiCwqP_ehbnSWfAwcGXwnEwaw2_KWX1bqttap1bK0', 'PIWKcwKwVuHIZA6IC8bXBPIZQ0BYY6T2Vp36Xc_obtY'],
                      success(res) {
                        console.log(res)
                        if (res["8lQiCwqP_ehbnSWfAwcGXwnEwaw2_KWX1bqttap1bK0"] === "accept" || res["PIWKcwKwVuHIZA6IC8bXBPIZQ0BYY6T2Vp36Xc_obtY"] === "accept") {
                          console.log(res);
                          wx.request({
                            url: yuming + 'Besall/subscribe_save',
                            method: 'POST',
                            header: {
                              "content-type": "application/x-www-form-urlencoded",
                              token: wx.getStorageSync('token'),
                            },
                            data: {

                            },
                            success: function (res) {
                              console.log(res)
                              if (res.data.code == 200) {
                                wx.setStorageSync('wen_is_ding', 1)
                                wx.setStorageSync('wen_istan', 1)
                              } else {
                                wx.showToast({
                                  title: res.data.msg,
                                  icon: 'none'
                                })
                                wx.navigateTo({
                                  url: '/page_zhu/shou/shou?sta=66',
                                })
                                wx.setStorageSync('wen_istan', 0)
                                wx.setStorageSync('wen_is_ding', 0)
                              }
                            },
                            fail: function () {
                              console.log("请求数据失败");
                            }
                          })
                        } else {
                          wx.setStorageSync('wen_istan', 1)
                        }
                      },
                      fail(er) {
                        console.log("订阅消息 失败 ");
                        console.log(er);
                        wx.setStorageSync('wen_is_ding', 0)
                        wx.setStorageSync('wen_istan', 0)
                      }
                    })

                  } else {
                    wx.setStorageSync('wen_is_ding', 0)
                    // 0 不弹 1弹
                    wx.setStorageSync('wen_istan', 1)
                  }
                }
              })
            } else {

            }

          }
        }
      }
    })



  },
  onShow() {
    if (this.data.currtab == 0) {
      this.setData({
        canis_show: false,
      })
    } else {
      this.setData({
        canis_show: true,
      })
    }
    this.wenxq()
    this.tubiao()
    this.shujua()
    if (wx.getStorageSync('wen_istan') == 0) {
      this.dingyue()
    }
  },
  // 提交
  tijiao() {
    // var wensx_bj = ''
    // var wenxx_bj = ''
    // var shisx_bj = ''
    // var shixx_bj = ''
    // var dianlbz_gj = ''
    // if (this.data.wensx_bj) {
    //   wensx_bj = 1
    // }
    // if (!this.data.wensx_bj) {
    //   wensx_bj = 0
    // }
    // if (this.data.wenxx_bj) {
    //   wenxx_bj = 1
    // }
    // if (!this.data.wenxx_bj) {
    //   wenxx_bj = 0
    // }
    // if (this.data.shisx_bj) {
    //   shisx_bj = 1
    // }
    // if (!this.data.shisx_bj) {
    //   shisx_bj = 0
    // }
    // if (this.data.shixx_bj) {
    //   shixx_bj = 1
    // }
    // if (!this.data.shixx_bj) {
    //   shixx_bj = 0
    // }
    // if (this.data.dianlbz_gj) {
    //   dianlbz_gj = 1
    // }
    // if (!this.data.dianlbz_gj) {
    //   dianlbz_gj = 0
    // }
    // if (this.data.caiji_jiange_guo < 60) {
    //   wx.showToast({
    //     title: '正常采集不能小于60秒哦',
    //     icon: 'none'
    //   })
    // } else if (this.data.shangc_jiange_guo < 60) {
    //   wx.showToast({
    //     title: '正常上传不能小于60秒哦',
    //     icon: 'none'
    //   })
    // } else if (this.data.chaowencj_jiange_guo < 60) {
    //   wx.showToast({
    //     title: '超温采集不能小于60秒哦',
    //     icon: 'none'
    //   })
    // } else if (this.data.chaowensc_jiange_guo < 60) {
    //   wx.showToast({
    //     title: '超温上传不能小于60秒哦',
    //     icon: 'none'
    //   })
    if (this.data.calTmp < -999 || this.data.calTmp > 999) {
      wx.showToast({
        title: '温度校准在-999和999之前',
        icon: 'none'
      })
    } else if (this.data.calHum < -999 || this.data.calHum > 999) {
      wx.showToast({
        title: '湿度校准在-999和999之前',
        icon: 'none'
      })
    } else {
      let that = this
      var wuphoto = that.data.wuphoto.toString()
      this.myRequest(yuming + 'Humidity/earlywarning_device_save', {
        shopId: wx.getStorageSync('shopId'),
        id: that.data.wen_id,
        // nickName: that.data.nickName,
        // collectionInterval: that.data.caiji_jiange_guos,
        // uploadInterval: that.data.shangc_jiange_guos,
        // overCollectionInterval: that.data.chaowencj_jiange_guos,
        // overUploadInterval: that.data.chaowensc_jiange_guos,
        // enableTmpUpperAlarm: wensx_bj,
        // tmpUpperLimit: that.data.baojingsx_guos,
        // enableTmpLowerAlarm: wenxx_bj,
        // tmpLowerLimit: that.data.baojingxx_guos,
        // enableHumUpperAlarm: shisx_bj,
        // humUpperLimit: that.data.baojingsxsd_guos,
        // enableHumLowerAlarm: shixx_bj,
        // humLowerLimit: that.data.baojingxxsd_guos,
        // enableQuantityAlarm: dianlbz_gj,
        // quantityLimit: that.data.dianlgjz_guos,
        calTmp: that.data.calTmp,
        calHum: that.data.calHum,
        // wuphoto: wuphoto
      }, "POST").then(res => {
        wx.showToast({
          title: res.data.msg,
          icon: 'none'
        })
        if (res.data.code == 200) {
          setTimeout(function () {
            that.wenxq()
          }, 500)
        }
      })
    }

  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },
  tubiao() {
    var that = this
    this.myRequest(yuming + 'Humidity/earlywarning_yujing_tubiao', {
      shopId: wx.getStorageSync('shopId'),
      id: that.data.she_id,
      page: that.data.page,
      // startime: that.data.startimes,
      // endtime: that.data.endtimes
    }, "POST").then(res => {
      console.log(res)
      var data = res.data.data
      //echarts
      console.log(res)
      var lie = res.data.data
      let shu_wendu = []
      let shu_shidu = []
      let riqi = []
      let array = []
      lie.forEach(item => {
        shu_wendu.push(item.temperature)
        shu_shidu.push(item.humidity)
        item.createtime = item.createtime.slice(0, 10)
        riqi.push(item.createtime)
        var y = {
          shijian: item.createtime,
          shi: item.humidity,
          wen: item.temperature
        }
        array.push(y)
      })
      console.log(array)
      if (array.length == 0) {
        this.setData({
          canis_show: true
        })
      }
      this.setData({
        array: array
      })
      this.firstComponent.init((canvas, width, height) => {
        const chart = echarts.init(canvas, null, {
          width: width,
          height: height
        });
        this.setOptionts(chart, shu_wendu, shu_shidu, riqi);
        return chart;
      });

    })
  },
  shujua() {
    var that = this
    this.myRequest(yuming + 'Humidity/earlywarning_yujing_alllist', {
      shopId: wx.getStorageSync('shopId'),
      id: that.data.she_id,
      page: that.data.page,
      startime: that.data.startimes,
      endtime: that.data.endtimes
    }, "POST").then(res => {
      console.log(res)
      var data = res.data.data
      if (this.data.page != 1) {
        if (data.length < 10) {
          wx.showToast({
            title: '没有更多数据了',
            icon: 'none'
          })
        }
      }
      this.setData({
        shuji: data,
        zongye: res.data.total
      })
    })
  },
  setOptionts: function (chart, w1, w2, w3) {
    var option = {
      tooltip: {
        trigger: 'axis'
      },
      legend: {
        data: ['温度', '湿度', ]
      },
      grid: {
        left: '3%',
        right: '10%',
        bottom: '15%',
        containLabel: true
      },
      dataZoom: [{
        type: 'slider',
        show: true,
        height: '5%',
        bottom: '5%',
        borderColor: '#8FDFFE',
        backgroundColor: '#B9D3EE',
        handleIcon: 'M512 512m-208 0a6.5 6.5 0 1 0 416 0 6.5 6.5 0 1 0-416 0Z M512 192C335.264 192 192 335.264 192 512c0 176.736 143.264 320 320 320s320-143.264 320-320C832 335.264 688.736 192 512 192zM512 800c-159.072 0-288-128.928-288-288 0-159.072 128.928-288 288-288s288 128.928 288 288C800 671.072 671.072 800 512 800z',
        handleColor: '#1C86EE',
        handleSize: 25,
        handleStyle: {
          borderColor: '#8FDFFE',
          shadowBlur: 4,
          shadowOffsetX: 1,
          shadowOffsetY: 1,
          shadowColor: '#e5e5e5'
        },
        start: 10,
        end: 50 //初始化滚动条
      }],
      xAxis: {
        type: 'category',
        boundaryGap: false,
        data: w3,
        // axisLabel: {
        //   interval: 0, //坐标刻度之间的显示间隔，默认就可以了（默认是不重叠）
        //   rotate: 10 //调整数值改变倾斜的幅度（范围-90到90）
        // }
      },
      yAxis: {
        type: 'value'
      },
      series: [{
          name: '温度',
          type: 'line',
          stack: 'Total',
          data: w1,

        },
        {
          name: '湿度',
          type: 'line',
          stack: 'Total',
          data: w2
        },
      ]

    };
    chart.setOption(option);
  },
  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },
})